package com.bcu.calculus.service.serviceImpl;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.bcu.calculus.config.GlobalConfig;
import com.bcu.calculus.entity.Message;
import com.bcu.calculus.entity.Send;
import com.bcu.calculus.entity.User;
import com.bcu.calculus.entity.UserAssist;

import com.bcu.calculus.mapper.SendMapper;
import com.bcu.calculus.mapper.UserMapper;
import com.bcu.calculus.mapper.UserinfoMapper;
import com.bcu.calculus.service.ExcelService;

import com.bcu.calculus.utils.GetLetterUtil;
import com.bcu.calculus.utils.SendUtil;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.*;

@Service
public class ExcelServiceImpl implements ExcelService {


    @Resource
    private GlobalConfig globalConfig;

    @Resource
    private SendMapper sendMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserinfoMapper userinfoMapper;

    @Override
    public Message getExcel(Integer semId) throws Exception {

        String topathname2 = "webapps/bcucalculusImage/chartnew/bcucalculus20200926.xlsx";


        //FileUtil.deleteFile(topathname2);

        //需要复制的目标文件或目标文件夹


        //FileUtil.copy("bcucalculus.xlsx","webapps/bcucalculusImage/chart","webapps/bcucalculusImage/chartnew");





        this.WriteExcel(semId);

        String returnUrl=globalConfig.hostUrl+ "bcucalculusImage/chart/" +"bcucalculus20200926.xlsx";

        return Message.success(null).add(returnUrl);
    }


    //根据用户和send确定写入的位置
    public void WriteExcelSingle(String userColumn, Send send,ExcelWriter writer) {


        // 通过工具类创建writer
        if (send.getSendTlistId()==null||send.getSendTlistId().equals("nolook")) {
            if (send.getSendSlistId()==null||send.getSendSlistId().equals("nolook")){
                if (send.getSendScore()!=null) {
                    writer.writeCellValue(userColumn + send.getSendFlistId(), send.getSendScore());
                }
            }else{
                if (send.getSendScore()!=null) {
                    writer.writeCellValue(userColumn + send.getSendSlistId(), send.getSendScore());
                }
            }
        } else {
            if (send.getSendScore()!=null) {
                writer.writeCellValue(userColumn + send.getSendTlistId(), send.getSendScore());
            }
        }
    }


    //获取所有user,按照党小组分类，返回Map类型数据，Key是第n党小组

    public  Map<Integer, List<UserAssist>> AssignmentColumn() {

        //通过mapper得到有几个党小组,则doubleList中有几个list
        List<Integer> branchList =  userinfoMapper.getBranchNumber();


        //去掉管理员的0分组
        branchList.remove((Integer)0);


        //获取User并放入Map中根据党小组分类
        Map<Integer, List<User>> userMap = new HashMap<>();
        for (Integer branch:branchList) {
            //通过Mapper获取，根据党小组字段查找
            List<User> userList = userMapper.findUserAndInfoByUserInfoBranch(branch);
            userMap.put(branch, userList);
        }

        //从第一个小组开始把User变成UserAssist类并赋予Column
        Map<Integer, List<UserAssist>> userAssistMap = new HashMap<>();
        int k = 0;
        for (Integer branch:branchList) {

            List<UserAssist> userAssistList = new ArrayList<>();
            List<User> userList = userMap.get(branch);
            for (User user : userList) {

                UserAssist userAssist = new UserAssist();
                userAssist.setUserId(user.getUserId());
                userAssist.setUserPwd(user.getUserPwd());
                userAssist.setUserType(user.getUserType());
                userAssist.setUserinfo(user.getUserinfo());
                userAssist.setUserColumn(GetLetterUtil.excelColIndexToStr(k+5));
                k++;
                userAssistList.add(userAssist);
            }
            userAssistMap.put(branch, userAssistList);
        }
        return userAssistMap;
    }

    public void WriteExcel(Integer semId){

        Map<Integer, List<UserAssist>> userAssistMap = this.AssignmentColumn();

        //根据用户查询用户所有的send
        for (int i = 1; i < userAssistMap.size()+1; i++) {
            //打开需要导入数据的Excel
            ExcelWriter writer = ExcelUtil.getWriter(globalConfig.filePath + "chart/bcucalculus20200926.xlsx");
            writer.setSheet("20200926");

            List<UserAssist> userAssistList = userAssistMap.get(i);
            if(userAssistList.size()==0) continue;
            String n = userAssistList.get(0).getUserColumn();

            int k = GetLetterUtil.excelColStrToNum(n,n.length());
            String head = "第"+i+"党小组";
            StyleSet style = writer.getStyleSet();



            writer.writeCellValue(userAssistList.get(0).getUserColumn()+"1",head);


            for (UserAssist userassist:userAssistList) {

                List<Send> sends = sendMapper.selectBySendUserId(userassist.getUserId(),semId);

                List<Send> newsends = SendUtil.changeSend(sends);

                if(i%2==0){
                    style.setBackgroundColor(IndexedColors.WHITE,false);
                }
                else {
                    style.setBackgroundColor(IndexedColors.AQUA,false);
                }


                //将名字填入Excel

                writer.writeCellValue(userassist.getUserColumn()+"2",userassist.getUserinfo().getUserName());

                for (Send send:newsends) {
                    if (send!=null){
                        if(send.getSendScore()!=null) {
                            this.WriteExcelSingle(userassist.getUserColumn(),send,writer);
                        }
                    }

                }
            }
            writer.close();
        }
    }


    public void WriteNameRow(ExcelWriter writer){
        Map<Integer, List<UserAssist>> userAssistMap = this.AssignmentColumn();
        for (int i = 1; i < userAssistMap.size()+1; i++) {
            List<UserAssist> userAssistList = userAssistMap.get(i);
            for (UserAssist userassist:userAssistList) {
                writer.writeCellValue(userassist.getUserColumn()+"2",userassist.getUserinfo().getUserName());

            }


        }
    }

}
